package com.kylexy.closes.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.kylexy.closes.R;
import com.kylexy.closes.app.AppContext;
import com.kylexy.closes.app.UIHelper;
import com.kylexy.closes.bean.CloseInfo;
import com.kylexy.closes.tools.MenuUtil;
import com.kylexy.closes.ui.base.BaseActivity;
import com.kylexy.closes.widge.photo.PhotoHelper;
import com.kylexy.closes.widge.photo.SinglePicture;
import com.kylexy.closes.widge.photo.SinglePicture.onPictureListener;
import com.lidroid.xutils.exception.DbException;
import com.tl.common.util.StringUtils;

public class ActivityCloseInfoEdit extends BaseActivity {

	CloseInfo _data;

	SinglePicture _photoes;
	String _picPath;
	boolean _isNew = true;

	EditText _etId, _etName;
	Button _btScaner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_closeinfo_edit);
		_context = this;

		Intent it = getIntent();
		if (it != null) {
			_data = (CloseInfo) it.getSerializableExtra("data");
			_isNew = false;
		} else {
			_data = new CloseInfo();
		}

		initTitle();
		initView();
		refreshData();
	}

	private void initTitle() {
		View _view = findViewById(R.id.top_bar);

		MenuUtil.setMenuBack(this, _view);
		MenuUtil.setMenuTitle(_view, _isNew ? "新增商品" : "编辑商品");

		MenuUtil.setMenuMore(this, _view, true, "保存", new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				saveData();
			}
		});
	}

	private void initView() {
		_photoes = (SinglePicture) findViewById(R.id.close_info_pic);

		// 保存的文件夹
		String path = AppContext.getInstance()._appImagePath;
		_photoes.setSavePath(path);
		_photoes.setPictureListener(picListener);
		_photoes.setPicture(null, true, true);

		// 扫描按钮
		_btScaner = (Button) findViewById(R.id.close_info_scaner);
		_btScaner.setOnClickListener(_scanListener);

		_etId = (EditText) findViewById(R.id.close_info_id);
		_etName = (EditText) findViewById(R.id.close_info_name);
	}

	private void refreshData() {
		if (_data == null || StringUtils.isEmpty(_data.getCloseId())) {
			return;
		}

		_btScaner.setVisibility(View.GONE);
		_etId.setEnabled(false);

		_etId.setText(_data.getCloseId());
		_etName.setText(_data.getCloseName());

		String pic = _data.getClosePic();
		if (!StringUtils.isEmpty(pic)) {
			_photoes.setImg(pic);
		}
	}

	onPictureListener picListener = new onPictureListener() {
		@Override
		public void onPictureSelected(String url) {
			_picPath = url;
		}
	};

	private void saveData() {
		if (!checkData())
			return;

		try {
			AppContext.getInstance().mDb.saveOrUpdate(_data);
		} catch (DbException e) {
			// TODO Auto-generated catch block
			UIHelper.ToastMessage(_context, "保存商品错误!/n" + e.getMessage());
			e.printStackTrace();
			return;
		}

		UIHelper.ToastMessage(_context, "保存商品成功!");
		finish();

	}

	/**
	 * 校验数据
	 * 
	 * @return
	 */
	private boolean checkData() {
		boolean isChecked = true;

		String id = _etId.getText().toString();
		if (StringUtils.isEmpty(id)) {
			_etId.requestFocus();
			UIHelper.ToastMessage(_context, "条码不能为空!");
			return false;
		}

		String name = _etName.getText().toString();
		if (StringUtils.isEmpty(name)) {
			_etName.requestFocus();
			UIHelper.ToastMessage(_context, "名称不能为空!");
			return false;
		}

		if (_data == null)
			_data = new CloseInfo();

		_data.setCloseId(id);
		_data.setCloseName(name);
		_data.setClosePic(_picPath);

		return isChecked;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == PhotoHelper.FROM_CAMERA
				|| requestCode == PhotoHelper.FROM_MEDIA) {
			_photoes.setOnActivityResult(requestCode, resultCode, data);
		}

		if (resultCode == CaptureActivity.FROM_SCANER) {
			_etId.setText(data.getStringExtra("id"));
		}

		super.onActivityResult(requestCode, resultCode, data);

	}

	private OnClickListener _scanListener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Intent it = new Intent(_context, CaptureActivity.class);
			startActivityForResult(it, CaptureActivity.FROM_SCANER);
		}
	};

}
